/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include <fstream>
#ifdef _WINDOWS
#include <cstdarg>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <strsafe.h>
#include "json/json.h"


int executeCmd(const char* _Cmd, ...)
{
    STARTUPINFOA _Si;
    PROCESS_INFORMATION _Pi;

    ZeroMemory(&_Si, sizeof(STARTUPINFOA));
    _Si.cb = sizeof(STARTUPINFOA);
    ZeroMemory(&_Pi, sizeof(PROCESS_INFORMATION));

    CHAR _SzCmd[0x1000];
    std::va_list _Ap;
    va_start(_Ap, _Cmd);
    StringCchVPrintfA(_SzCmd, ARRAYSIZE(_SzCmd), _Cmd, _Ap);
    va_end(_Ap);

    if (CreateProcessA(NULL, _SzCmd, NULL, NULL, 
        FALSE, FALSE, NULL, NULL, &_Si, &_Pi)) {
            WaitForSingleObject(_Pi.hProcess, INFINITE);
            return 0;
    }
    return int(GetLastError());
}
#endif


bool parseFile(const char* _File, Json::Value& _Root)
{
    Json::Reader _Reader;
    std::ifstream _Is;
    _Is.open(_File, std::ios::binary);
    if (_Is.is_open()) {
        if (_Reader.parse(_Is, _Root, FALSE)) {
            return true;
        }
    }
    return false;
}